package vn.gamesv.stores;

import vn.gamesv.stores.adapter.ImageAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GameSVMainActivity extends Activity {

	GridView gridView;
	String[] button_screen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_svmain);
		gridView = (GridView) findViewById(R.id.gridHomeScreen);
		Resources resources = getResources();
		button_screen = resources.getStringArray(R.array.button_screen);
		gridView.setAdapter(new ImageAdapter(GameSVMainActivity.this,
				button_screen));

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {

				switch (position) {
				case 0:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVRomantic.class));
					break;
				case 1:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVFunny.class));
					break;
				case 2:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVSachHay.class));
					break;
				case 3:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVTiengAnh.class));
					break;
				case 4:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVCamNang.class));
					break;
				case 5:
					startActivity(new Intent(GameSVMainActivity.this,
							GameSVGames.class));
					break;

				default:
					break;
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game_svmain, menu);
		return true;
	}

	/*
	 * Confirm exit application
	 */
	public void showDialogConfirmExit(final Context context,
			final Activity activity, String title, String message,
			final boolean end) {
		final AlertDialog alertDialog = new AlertDialog.Builder(context)
				.create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton("Đồng ý", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				// here you can add functions
				if (end)
					activity.finish();
				alertDialog.dismiss();
			}
		});
		alertDialog.setButton2("Đóng", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				// here you can add functions
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

	// /////////////////////////////////////////////////
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				System.out.println("ON KEY BACK");
				showDialogConfirmExit(this, GameSVMainActivity.this,
						"Thông báo", "Bạn có muốn thoát khỏi ứng dụng?", true);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
